import axios from "axios"
import Izitoast from 'izitoast'
import router from '../router'

const service = axios.create({
    baseURL: "/api",
    timeout: 20000
})
service.interceptors.request.use(
    config => {
        const token = localStorage.getItem("token");
        token && (config.headers.Authorization = token);
        token && (config.headers.token = token);
        console.log({ 请求体信息: config })
        return config
    },
    error => {
        console.log("请求失败:" + error)
        Promise.reject(error)
    }
)
service.interceptors.response.use(
    response => {
        if (response.data.code === 200) {
            return response.data
        } else if (response.data.code === 1000) {
            // window.localStorage.removeItem("token");
            router.push({
                path: "../login"
            });
            return response.data
        } else {
            Izitoast.warning({
                iconUrl: "../../static/img/robot.png",
                position: "topCenter",
                title: "开发助手",
                message: response.data.msg
            });
            return response.data
        }

    },
    error => {
        Izitoast.warning({
            iconUrl: "../../static/img/robot.png",
            position: "topCenter",
            title: "系统Bug",
            message: "我们已通知程序员小哥哥加紧修复"
        });
        return Promise.reject(error)
    }
)
export default service